package com.gouda.backend.tag;

import com.google.gdata.util.common.base.StringUtil;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: souu
 * Date: 15-3-30
 * Time: 下午2:57
 * To change this template use File | Settings | File Templates.
 */
public class PageTag extends TagSupport {

    private static final long serialVersionUID = 1L;
    private static StringBuffer tags_html;
    //设置左右显示页码数
    final private static int number = 2;
    //请求URI,格式如：/user/message
    private String sendUri;
    //总页码
    private Integer pageSize;
    //当前页面
    private Integer pageNo;
    //其他参数
    private String param;

    @Override
    public void release() {
        super.release();
    }

    @Override
    public int doStartTag() throws JspException {
        if (!StringUtil.isEmptyOrWhitespace(sendUri)) {
            tags_html = new StringBuffer();
//            if (sendUri.toLowerCase().startsWith("javascript")) {
//                //取消javascript
//                sendUri = sendUri.substring(10, sendUri.length());
//                if (sendUri.startsWith(":")) {
//                    sendUri = sendUri.substring(1, sendUri.length());
//                }
//                if (!StringUtil.isEmptyOrWhitespace(param)) {
//                    ajaxUri(tags_html, sendUri, param);
//                }
//            } else {
            httpUri(tags_html);
//            }
        }
        return TagSupport.EVAL_PAGE;
    }

    /**
     * HTTP URI GET 方式请求调用
     *
     * @param html
     */
    void httpUri(StringBuffer html) {
        html.append(" <div class='left' id='numWrap'>");
        if (pageNo > 1) {
//            html.append("<div class='leftBtn'><a href='" + sendUri + "?page=" + (page - 1) + "' ><img src='../../images/cms/left.gif'/></a></div>");
            html.append("<div class='leftBtn'><a href='" + sendUri + "?pageNo=" + (pageNo - 1) + "' ><</a></div>");
        }
        if (pageNo <= 1) {
//            html.append("<div class='leftBtn'><img src='../../images/cms/left.gif'/></div>");
            html.append("<div class='leftBtn'><</div>");
        }
        html.append("<div class='num'>");
        boolean isNext = true;
        //如果总页码在 1...?page?...pageSize 的范围内，直接全部显示（?为im2Page.numberMuch变量）
        if (pageSize < (1 + (number * 2) + 1 + 1)) {
            for (int i = 1; i <= pageSize; i++) {
                if (pageNo == i) {
                    html.append("<span class='hover'>" + i + "</span>");
                } else {
                    html.append("<span><a href='" + sendUri + "?pageNo=" + i + param + "'>" + i + "</a></span>");
                }
            }
            isNext = false;
        }
        if (isNext) {

            //最小数（当前page-number）
            int min = 0;
            //最大数（当前page-number）
            int max = 0;

            if ((pageNo - number) > 0) {
                min = pageNo - number;
            } else {
                min = 1;
            }
            if ((pageNo + number) < pageSize) {
                max = pageNo + number;
            } else {
                max = pageSize;
            }

            if (pageNo <= (number - 1)) {
                max = number + 1;
            } else {
                if ((pageNo - 1) < number) {
                    max = max + number;
                }
                if ((pageNo - 1) == number) {
                    max = max + (number - 1);
                }
            }

            if (pageSize == pageNo) {
                min = min - number;
            }
            if ((pageSize - pageNo) < (number)) {
                min = min - (pageSize - pageNo) - 1;
            }
            if ((pageSize - pageNo) == (number)) {
                min = min - (number - 1);
            }

            if (min > 1) {
                html.append("<span><a href='" + sendUri + "?pageNo=1" + param + "'>1</a></span>");
            }
            if ((min - 1) > 1) {
                html.append("&nbsp;...&nbsp;");
            }
            for (int i = min; i <= max; i++) {
                if (pageNo == i) {
                    html.append("<span class='hover'>" + i + "</span>");
                } else {
                    html.append("<span><a href='" + sendUri + "?pageNo=" + i + param + "'>" + i + "</a></span>");
                }
            }
            if ((max + 1) < pageSize) {
                html.append("&nbsp;...&nbsp;");
            }
            if (max < pageSize) {
                html.append("<span><a href='" + sendUri + "?pageNo=" + pageSize + param + "'>" + pageSize + "</a></span>");
            }
        }
        html.append("</div>");
        if (pageNo < pageSize) {
//            html.append("<div class='rightBtn'><a href='" + sendUri + "?page=" + (page + 1) + param + "'><img src='../../images/cms/right.gif'/></a></div>");
            html.append("<div class='rightBtn'><a href='" + sendUri + "?pageNo=" + (pageNo + 1) + param + "'>></a></div>");
        }
        if (pageNo >= pageSize) {
//            html.append("<div class='rightBtn'><img src='../../images/cms/right.gif'/></div>");
            html.append("<div class='rightBtn'>></div>");
        }
        html.append("</div>");
        try {
            pageContext.getOut().print(html);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getSendUri() {
        return sendUri;
    }

    public void setSendUri(String sendUri) {
        this.sendUri = sendUri;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

}
